package com.example.administrator.testc.WebViewCacahe;

import android.os.Looper;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.Map;

/**
 * Created by xcp on 16/3/2.
 */
public class MyHttpClient extends Thread {

    private static final String CHILD_TAG = "ChildThread";

    private Class cls;
    private IHttpStr iHttpStr;
    private String tag;
    private String remoteUrl;
    private String BroadCastKey;
    private String reqType = "GET";
    private Map<String, Object> dataMap;
    private int dataType = FROM_TYPE; // JSON 1 or FROM 2
    public static final int JSON_TYPE = 1;
    public static final int FROM_TYPE = 2;

    public void run() {
        //初始化消息循环队列，需要在Handler创建之前
        Looper.prepare();
        this.setName("ChildThread");
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        try {
            com.squareup.okhttp.Request.Builder builder = new com.squareup.okhttp.Request.Builder();
            builder = builder.url(remoteUrl);
            builder = builder.addHeader("Accept", "application/json; q=0.5");
            builder = builder.get();

            com.squareup.okhttp.Request request = builder.build();

            Response response = client.newCall(request).execute();
            String jsonString = response.body().string();

//            Log.i("TAGSS MyHttp", "remoteUrl : " + remoteUrl);
//            Log.i("TAGSS MyHttp", "jsonString : " + jsonString);
            if(iHttpStr != null)
                iHttpStr.onResult(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                client = null;
            }
        }

        //启动子线程消息循环队列
        Looper.loop();
    }

//    public interface IHttp {
//        void onListener(JsonBean bean, String response);
//    }

    public interface IHttpStr{
        void onResult(String jsonStr);
    }

    public MyHttpClient getDate(Class cls, String BroadCastKey, String tag) {
        this.cls = cls;
        this.tag = tag;
        this.BroadCastKey = BroadCastKey;
        return this;
    }

    public int getDataType() {
        return dataType;
    }

    public MyHttpClient setDataType(int dataType) {
        this.dataType = dataType;
        return this;
    }

    public MyHttpClient setSendDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
        return this;
    }

    public MyHttpClient setReqType(String reqType) {
        this.reqType = reqType;
        return this;
    }

    public MyHttpClient setBroadCastKey(String broadCastKey) {
        BroadCastKey = broadCastKey;
        return this;
    }

    public MyHttpClient setTag(String tag) {
        this.tag = tag;
        return this;
    }

    public MyHttpClient setiHttpStr(IHttpStr iHttpStr) {
        this.iHttpStr = iHttpStr;
        return this;
    }

    public MyHttpClient setCls(Class cls) {
        this.cls = cls;
        return this;
    }

    public MyHttpClient setRemoteUrl(String remoteUrl) {
        this.remoteUrl = remoteUrl;
        return this;
    }

    public MyHttpClient setDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
        return this;
    }
}
